﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BehaviorSystem;

public class PlayerMoveNode : ActionNode {

    PlayerMoveComponent move;
    AnimatorComponnent animatorComponnent;

    float distance;
    public PlayerMoveNode(IBehaviorAgent agent) : base(agent)
    {
        move = agent.GetComponent<PlayerMoveComponent>();
        animatorComponnent = agent.GetComponent<AnimatorComponnent>();
    }

    public override BTNodeState HandleAction()
    {
        //if (PlayerManager.Inst.CurControlHero!=null&&PlayerManager.Inst.CurControlHero.gameObject==move.gameObject)
        {
            if (move.IsCanMove)
            {
                if (move.targetPos != Vector3.zero)
                {
                    distance = Vector3.Distance(move.transform.position, move.targetPos);
                    if (distance > 0.1 && move.targetPos != Vector3.zero)
                    {
                        animatorComponnent.SetBool("Run", true);
                        move.transform.LookAt(move.targetPos);
                        move.transform.position = Vector3.MoveTowards(move.transform.position, move.targetPos, move.Speed);
                    }
                    else
                    {
                        move.targetPos = Vector3.zero;
                        animatorComponnent.SetBool("Run", false);

                    }
                }
                else
                {
                    CurState = BTNodeState.Success;
                }
            }
            else
            {
                CurState = BTNodeState.Failure;
            }
        }
        //else
        {
            CurState = BTNodeState.Invalid;
        }

        return CurState;
    }

    public override void OnReseted()
    {
        animatorComponnent.SetBool("Run", false);
    }
}
